{template 'common/header'}
<script>
    $(function(){
        var finish = "{$finish['id']}";
        if(finish > 0){
            alert("当日订单已经完成!");
        }

        $("#day").on('change',function(){
            var day = $(this).val();
            var url = "{php echo $this->createWebUrl('orders',array('op' => 'check_finish'))}";
            $.ajax({
                type : 'post',
                url : url,
                data : {'day' : day},
                success : function(data){
                    if(data == 111){
                        alert("该天订单已经完成！");
                    }
                }
            });
        });
        $("#key-word").focus();
        $(".key-word").keyup(function(){
            var key_word = $(this).val();
            var region_id = $(".region").val();
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'find_member'))}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'key_word' : key_word, 'region_id' : region_id},
                    success : function(data){
                        $("#member-list").html(data);
                    }
                });
        });
        $(".region").change(function(){
            var region_id = $(this).val();
            var key_word = $("#key-word").val();
            var order_region = $("#order-region").val();
            location.href = "{php echo $this->createWebUrl('orders',array('op' => 'add'))}&region_id="+region_id+"&key_word="+key_word+"&order_region="+order_region;
        });
        $(".order-region").change(function(){
            var region_id = $("#region-id").val();
            var key_word = $("#key-word").val();
            var order_region = $(this).val();
            location.href = "{php echo $this->createWebUrl('orders',array('op' => 'add'))}&region_id="+region_id+"&key_word="+key_word+"&order_region="+order_region;
        });
        $(".member").dblclick(function(){
            var member_id = $(this).attr("member_id");
            var day = $("#day").val();
            var order_region = $("#order_region").val();
            if(member_id > 0){
                location.href="{php echo $this->createWebUrl('orders',array('op' => 'add_order'))}&member_id="+member_id+"&order_region="+order_region+"&day="+day;
            }
        });
        $(".choose-member").click(function(){
            var member_id = $(this).attr("member_id");
            var day = $("#day").val();
            var order_region = $("#order-region").val();
            if(member_id > 0){
                location.href="{php echo $this->createWebUrl('orders',array('op' => 'add_order'))}&member_id="+member_id+"&order_region="+order_region+"&day="+day;
            }
        });
        $(".once-sure").click(function(){
            var once_name = $("#once-name").val();
            var once_phone = $("#once-phone").val();
            var once_region = $("#order-region").val();
            var day = $("#day").val();
            if(once_name != '' && once_phone != '' && once_region > 0){
                var url = "{php echo $this->createWebUrl('orders',array('op' => 'once_member'))}";
                $.ajax({
                    type : 'post',
                    url : url,
                    data : {'once_name' : once_name, 'once_phone' : once_phone, 'once_region' : once_region,'day' : day},
                    success : function(data){
//                        alert(data);
                        var json = JSON.parse(data);
                        if(json.errorCode == 0){
                            location.href="{php echo $this->createWebUrl('orders',array('op' => 'order_goods_add'))}&order_id="+json.order_id;
                        }else{
                            alert(json.msg);
                        }
                    }
                });
            }else{
                alert("请填写一次性客户的相关内容");
            }
        });
    });
    function choose_member(member_id){
        var day = $("#day").val();
        var order_region = $("#order-region").val();
        if(member_id > 0) {
            location.href = "{php echo $this->createWebUrl('orders',array('op' => 'add_order'))}&member_id=" + member_id + "&order_region=" + order_region + "&day=" + day;
        }
    }
</script>
<ul class="nav nav-tabs">
    <li ><a href="{php echo $this->createWebUrl('orders', array('op'=>'index'))}">订单列表</a></li>
    <li  class="active"><a href="{php echo $this->createWebUrl('orders', array('op'=>'add'))}">新建订单</a></li>
    <li ><a href="{php echo $this->createWebUrl('orders', array('op'=>'system'))}">待付款手续费设置</a></li>
</ul>
<div class="main">
    <!--<form action="" method="post" class="form-horizontal form" enctype="multipart/form-data">-->
        <div class="panel panel-default">
            <div class="panel-heading">
                新增订单
            </div>
            <div class="panel-body">
                <div class="row">
                    <div class="col-xs-3">搜索客户</div>
                    <div class="col-xs-3">订单日期</div>
                    <div class="col-xs-3">客户区域</div>
                    <div class="col-xs-3">到货区域（不选默认客户所在区域）</div>
                </div>
                <div class="row">
                    <div class="col-xs-3"><input type="text" value="{$key_word}" placeholder="客户名称/简称/电话" class="form-control key-word" id="key-word" /></div>
                    <div class="col-xs-3">
                        <input style="color:red;" type="date" value="{$day}" class="form-control" id="day" />
                    </div>
                    <div class="col-xs-3">
                        <select class="form-control region" id="region-id">
                            <option>请选择区域</option>
                            {loop $regions $region}
                            <option value="{$region['id']}" {if $region['id'] == $region_id} selected='selected' {/if}>{$region['name']}</option>
                            {/loop}
                        </select>
                    </div>
                    <div class="col-xs-3">
                        <select class="form-control order-region" id="order-region">
                            <option value="0">请选择到站区域</option>
                            {loop $regions $region}
                            <option value="{$region['id']}" {if $region['id'] == $order_region} selected='selected' {/if}>{$region['name']}</option>
                            {/loop}
                        </select>
                    </div>
                </div>
                <br />
                <div class="row">
                    <div class="col-xs-3">
                        <input type="text" placeholder="客户名称" class="form-control" id="once-name" />
                    </div>
                    <div class="col-xs-3">
                        <input type="text" placeholder="客户电话" class="form-control" id="once-phone" />
                    </div>
                    <div class="col-xs-3">
                        <button class="btn btn-warning once-sure">新增一次性客户</button>
                    </div>
                </div>
                <br />
                <div class="row" style="padding-left: 10px;">
                    <a href="{php echo $this->createWebUrl('members',array('op' => 'add','order_add' => 1))}">
                        <button class="btn btn-primary">新增客户</button>
                    </a>
                    <!--<button class="btn btn-warning" data-toggle="modal" data-target="#myModal">新增一次性客户</button>-->
                </div>
                <br />
                <div>
                    <table class="table" id="member-list">
                        <tr >
                            <th>编号</th>
                            <th>客户名称</th>
                            <th>客户电话</th>
                            <th>客户区域</th>
                            <th>备注</th>
                        </tr>
                        {loop $list $l}
                        <tr class="member" member_id="{$l['id']}">
                            <td>
                                {$l['key']}
                            </td>
                            <td>
                                <button class="btn btn-primary choose-member" member_id="{$l['id']}">{$l['name']}</button>
                            </td>
                            <td>{$l['phone']}</td>
                            <td>{$l['region']}</td>
                            <td>{$l['remark']}</td>
                        </tr>
                        {/loop}
                    </table>
                </div>

            </div>
        </div>
</div>
<!-- Modal -->
<!--<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">-->
    <!--<div class="modal-dialog" role="document">-->
        <!--<div class="modal-content">-->
            <!--<div class="modal-header">-->
                <!--<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>-->
                <!--<h4 class="modal-title" id="myModalLabel">请填写一次性客户资料</h4>-->
            <!--</div>-->
            <!--<div class="modal-body">-->
                <!--<div class="col-xs-3"></div>-->
                <!--<div class="col-xs-6">-->
                    <!--<input type="text" placeholder="客户名称" class="form-control" id="once-name" />-->
                    <!--<br />-->
                    <!--<input type="text" placeholder="客户电话" class="form-control" id="once-phone" />-->
                    <!--<br />-->
                    <!--<select class="form-control" id="once-region">-->
                        <!--<option >请选择区域</option>-->
                        <!--{loop $regions $region}-->
                        <!--<option value="{$region['id']}" >{$region['name']}</option>-->
                        <!--{/loop}-->
                    <!--</select>-->
                <!--</div>-->
                <!--<div class="col-xs-3"></div>-->
            <!--</div>-->
            <!--<div class="modal-footer">-->
                <!--<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>-->
                <!--<button type="button" class="btn btn-primary once-sure">确定</button>-->
            <!--</div>-->
        <!--</div>-->
    <!--</div>-->
<!--</div>-->

{template 'common/footer'}